unit fBrowserColumnsConfig;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxLookAndFeelPainters, cxControls,
  cxContainer, cxEdit, cxGroupBox, cxCheckGroup, cxCheckListBox, Menus,
  ExtCtrls, StdCtrls, cxButtons,uPublic, cxGraphics, cxLookAndFeels,
  dxSkinsCore, dxSkinCaramel, dxSkinOffice2010Silver, dxSkinSeven;

type
  TfmBrowserColumnsConfig = class(TForm)
    ckbVisibleColumns: TcxCheckListBox;
    btnOk: TcxButton;
    cxButton2: TcxButton;
    btnDefault: TcxButton;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
  private
    { Private declarations }
    function HasModalRight(ARight: TBillOperationRight):Boolean;    
  public
    { Public declarations }
    FColumnsList: TStrings;
    FColumnNamesList: TStrings;
    FVisibleColumnsList: TStrings;
    FModal: string;
    FVisibleResult: string;
    procedure Initialize(AColumns: string;AColumnNames:string;APriceColumns:string;ACostColumns:string;AVisibleColumns:string);
  end;

var
  fmBrowserColumnsConfig: TfmBrowserColumnsConfig;

implementation

uses
  fClientDataModule,DB,tools;

{$R *.dfm}
function TfmBrowserColumnsConfig.HasModalRight(ARight: TBillOperationRight):Boolean;
begin
  Result := False;
  with ClientDataModule do
  begin
    dmdPower.Locate('Modal',FModal, [loPartialKey]);
    case ARight of
      borCost: Result := dmdPowerCostValid.AsBoolean;
      borPrice: Result := dmdPowerPriceValid.AsBoolean;
    end;
  end;

end;

procedure TfmBrowserColumnsConfig.Initialize(AColumns: string;AColumnNames:string;APriceColumns:string;ACostColumns:string;AVisibleColumns:string);
var
  i,j: Integer;
  pricecolumnslist,costcolumnslist: TStrings;
begin
  pricecolumnslist := TStringList.Create;
  costcolumnslist := TStringList.Create;
  ExtractStrings([','],[' '],PChar(AColumns),FColumnsList);
  ExtractStrings([','],[' '],PChar(AColumnNames),FColumnNamesList);  
  ExtractStrings([','],[' '],PChar(APriceColumns),pricecolumnslist);
  ExtractStrings([','],[' '],PChar(ACostColumns),costcolumnslist);
  ExtractStrings([','],[' '],PChar(AVisibleColumns),FVisibleColumnsList);

  if not HasModalRight(borPrice) then
  begin
    for i := 0 to pricecolumnslist.Count - 1 do
    begin
      j := FColumnsList.IndexOf(pricecolumnslist.Strings[i]);
      if j <> -1 then
      begin
        FColumnsList.Delete(j);
        FColumnNamesList.Delete(j);
      end;
    end;
  end;
  if not HasModalRight(borCost) then
  begin
    for i := 0 to costcolumnslist.Count - 1 do
    begin
      j := FColumnsList.IndexOf(costcolumnslist.Strings[i]);
      if j <> -1 then
      begin
        FColumnsList.Delete(j);
        FColumnNamesList.Delete(j);
      end;      
    end;
  end;  

  FreeAndNil(pricecolumnslist);
  FreeAndNil(costcolumnslist);
end;

procedure TfmBrowserColumnsConfig.btnOkClick(Sender: TObject);
var
  i: Integer;
begin
  FVisibleResult := '';
  for i := 0 to ckbVisibleColumns.Count - 1 do
  begin
    if ckbVisibleColumns.Items[i].Checked then
      FVisibleResult := FVisibleResult + FColumnsList.Strings[i] + ',';
  end;
  Delete(FVisibleResult , length(FVisibleResult) ,1);
  ModalResult := mrOk;

end;

procedure TfmBrowserColumnsConfig.FormCreate(Sender: TObject);
begin
  FColumnsList := TStringList.Create;
  FVisibleColumnsList := TStringList.Create;
  FColumnNamesList := TStringList.Create;
end;

procedure TfmBrowserColumnsConfig.FormDestroy(Sender: TObject);
begin
  FColumnsList.Free;
  FVisibleColumnsList.Free;
  FColumnNamesList.Free;
end;

procedure TfmBrowserColumnsConfig.FormShow(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to FColumnsList.Count - 1 do
  begin
    ckbVisibleColumns.AddItem(FColumnNamesList.Strings[i]);
    if FVisibleColumnsList.IndexOf(FColumnsList.Strings[i]) <> -1 then
      ckbVisibleColumns.Items[i].Checked := True
    else
      ckbVisibleColumns.Items[i].Checked := False;
  end;
end;

end.
